<?
session_start();
if($_SESSION["logged"]){session_unset();session_destroy();}

    include "validator.php";
    include "sqlManager.php";

    $validator = new Validator();
    $sqlManager = new sqlManager();

    if(isset($_POST[login])){
        if(empty($_POST[name])){
            $report['name'] = "Vyplnte jmeno";
        }
        if(empty($_POST[username])){
            $report['username'] = "Vyplnte username";
        }
        if(empty($_POST[password])){
            $report['passwordEmpty'] = "Vyplne heslo.";
        }
        if($sqlManager->isUserNameUsed($_POST[username])){
            $report['usernameIsUsed'] = "Username je uz pouzito, pouzijte prosim jine.";
        }
        if($validator->validAlfanumeric($_POST[username]) && $validator->validSpace($_POST[username])){
            $report['wrongUsername'] = "Username smí obsahovat pouze alfanumerické znaky, vykřičník (!), znak dolaru ($), znak (#) a musi byt bez mezer.";
        }
        if($_POST[password] == $_POST[username]){
            $report['sameUsernameAsPassword'] = "Heslo nesmi byt stejne jako uzivatelske jmeno.";
        }
        if(strlen($_POST[password])<5){
            $report['shortPassword'] = "Heslo je prilis kratke.";
        }
        if($validator->validAlfanumeric($_POST[password]) && $validator->validSpace($_POST[password])){
            $report['wrongPassword'] = "Heslo smí obsahovat pouze alfanumerické znaky, vykřičník (!), znak dolaru ($), znak (#) a musi byt bez mezer.";
        }
        if(!$validator->validEmail($_POST[email])){
            $report['password1'] = "Neplatny email.";
        }
        if(empty($report)){
            $sqlManager->register($_POST[name], $_POST[username], $_POST[password], $_POST[email]);
            $final = "Uspesne zaregistrovano. Za chvili budete prihlaseni.";
            session_start();
            $_SESSION = Array();
            $_SESSION['logged']=true;
            $_SESSION['name']=$_POST[name];
            $_SESSION['username']=$_POST[username];
            ?>
                <meta http-equiv="refresh" content="2;url=http://databaze.fbmi.cvut.cz/fialadom/BITWA/CV5/CV5_bmi_db_SQL.php">
            <?
        }else{
            $_SESSION['logged']=false;
            unset($final);
        }
    }

  
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
  <meta http-equiv="content-type" content="text/html; charset=utf-8">
  <meta name="zajimaveeeeeee" content="PhpStorm 7.0">
  <link rel="stylesheet" type="text/css" href="style.css">
  <title>BITWA - Fiala Dominik</title>

  </head>
  <body>
    <div id="hlavicka">  
      <h1 align="center">BITWA</h1><br>      
    </div>
    <div id="menu">
      <? include "menu.php" ?>
    </div>
    <div id="obsah">
        <form method="POST" action="" >
	
        <label for="name"> Jmeno </label>
	<input type="text" id="name" name="name" placeholder="name" value="<?php echo $_POST['name'];?>">
	
	<br/>
	<label for="username"> Username </label>
	<input type="text" id="username" name="username" placeholder="username" value="<?php echo $_POST['username'];?>">

    <br/>
    <label for="email"> Email </label>
    <input type="text" id="email" name="email" placeholder="email" value="<?php echo $_POST['email'];?>">

            <br/>
	<label for="password"> Heslo </label>
	<input type="password" id="password" name="password" ">
        
	<input type="submit" name="login" value="Register">
        </form>
      <br><b>
    <?php
        foreach($report as $ch){
            echo "<b>".$ch."</b><br>";
        }
        echo $final;
    ?>
    </b>
    </div>
    <div id="copyright">
      &copy fialadom
    </div>  
  </body>
</html>